using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class MemoClearerSpecification {
        [Test]
        public void ShouldClearMemoResultsForEachProduction () {
            char[] source = "sldkf".ToCharArray();
            IProduction prod1 = CreateProductionWithMemoResult (3, source);
            IProduction prod2 = CreateProductionWithMemoResult (3, source);

            var clearer = new MemoClearer (new [] {prod1, prod2});

            clearer.ClearMemoFor (3, source);

            Assert.False (prod1.GetMemoTable (source).ContainsKey (3));
            Assert.False (prod2.GetMemoTable (source).ContainsKey (3));
        }

        private static IProduction CreateProductionWithMemoResult (int index, char [] source) {
            var prod1 = new Mock<IProduction> ();
            var memoTable = new Dictionary<int, ParseResult> ();
            prod1.Setup (p => p.GetMemoTable (source)).Returns (memoTable);
            memoTable[index] = new ParseResult (4, null, null);
            return prod1.Object;
        }
    }
}